const api = require('../../utils/api.js');
const app = getApp();

let is_no_more = false;
let is_loading = false;
let p = 2;

Page({
  data: {
    order_list: [],
    show_no_data_tip: false,
    abnorData: {
      imgSrc: '/common/static/images/abnor-10.png',
      text: '还没有相关公告哦',
    },
  },

  onLoad(opt) {
    let that = this;
    that.setData({
      from: opt.from,
    });
    app.pageOnLoad(this);
    is_no_more = false;
    is_loading = false;
    p = 2;
    wx.setNavigationBarTitle({
      title: '公告列表',
    });
    that.loadOrderList();
  },

  onShow() {},

  onReachBottom() {
    let that = this;
    if (is_loading || is_no_more) {
      return;
    }
    is_loading = true;
    wx.showLoading({
      title: '加载中...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.default.noticelist,
      data: {
        shop_id:
          that.data.from === 'shop' ? app.ws_micro_shop_globalData.shopid : 0,
        page: p,
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          let order_list = that.data.order_list.concat(res.data.notice);
          that.setData({
            order_list: order_list,
          });
          if (res.data.notice.length === 0) {
            is_no_more = true;
            // wx.showToast({
            //   title: '没有更多数据了',
            //   icon: 'none',
            // });
          }
        }
        p++;
      },
      complete: function() {
        is_loading = false;
      },
    });
  },

  loadOrderList() {
    let that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.default.noticelist,
      data: {
        shop_id:
          that.data.from === 'shop' ? app.ws_micro_shop_globalData.shopid : 0,
        page: 1,
      },
      success: function(res) {
        if (res.code === 0) {
          that.setData({
            order_list: res.data.notice,
          });
        }
        that.setData({
          show_no_data_tip: that.data.order_list.length === 0,
        });
      },
      complete: function() {
        wx.hideLoading();
      },
    });
  },

  onNavigator(e) {
    app.util.navigateTo(
      '/ws_micro_shop/pages/article-detail/article-detail',
      { id: e.currentTarget.dataset.id, type: 'notice' },
      false
    );
  },
});
